package jdbc;

import java.sql.*;

public class JDBCDemo1 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        //加载驱动
        //加载MySQL驱动
        Class.forName("com.mysql.cj.jdbc.Driver");
        //通过DriverManger与数据库建立连接
        Connection connection = DriverManager.getConnection(
                                            //数据库名
                "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                "root",
                "root");
        System.out.println("与数据库连接成功!");
        //通过连接对象Connection创建语句对象Statement
        Statement statement = connection.createStatement();
        String sql = "CREATE TABLE userinfo\n" +
                "(\n" +
                "    id       INT PRIMARY KEY AUTO_INCREMENT,\n" +
                "    username VARCHAR(30),\n" +
                "    password VARCHAR(30),\n" +
                "    nickname VARCHAR(30),\n" +
                "    age INT(3)\n" +
                ")";
        //通过statement执行sql语句 execute()方法可以执行所有类型的sql语句
        //                       返回一个boolean值判断是否有查询到数据并返回数据 若是sql语句执行不成功会直接报错
        boolean back = statement.execute(sql);
        System.out.println(back);
        System.out.println("创建表成功！");
        connection.close();
    }

}
